home *** CD-ROM | disk | FTP | other *** search
-
-
-
- LLLLSSSSEEEEAAAARRRRCCCCHHHH((((3333CCCC)))) LLLLSSSSEEEEAAAARRRRCCCCHHHH((((3333CCCC))))
-
-
-
- NNNNAAAAMMMMEEEE
- lsearch, lfind - linear search and update
-
- SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
- ####iiiinnnncccclllluuuuddddeeee <<<<ssssttttddddiiiioooo....hhhh>>>>
- ####iiiinnnncccclllluuuuddddeeee <<<<sssseeeeaaaarrrrcccchhhh....hhhh>>>>
-
- vvvvooooiiiidddd ****llllsssseeeeaaaarrrrcccchhhh ((((((((ccccoooonnnnsssstttt vvvvooooiiiidddd ****))))kkkkeeeeyyyy,,,, ((((vvvvooooiiiidddd ****))))bbbbaaaasssseeee,,,,
- ssssiiiizzzzeeee____tttt ****nnnnmmmmeeeemmmmbbbb,,,, ssssiiiizzzzeeee____tttt ssssiiiizzzzeeee,,,,
- iiiinnnntttt ((((****ccccoooommmmppppaaaarrrr))))((((ccccoooonnnnsssstttt vvvvooooiiiidddd ****,,,, ccccoooonnnnsssstttt vvvvooooiiiidddd ****))))))));;;;
-
- vvvvooooiiiidddd ****llllffffiiiinnnndddd ((((((((ccccoooonnnnsssstttt vvvvooooiiiidddd ****))))kkkkeeeeyyyy,,,, ((((ccccoooonnnnsssstttt vvvvooooiiiidddd ****))))bbbbaaaasssseeee,,,,
- ssssiiiizzzzeeee____tttt ****nnnnmmmmeeeemmmmbbbb,,,, ssssiiiizzzzeeee____tttt ssssiiiizzzzeeee,,,,
- iiiinnnntttt ((((****ccccoooommmmppppaaaarrrr))))((((ccccoooonnnnsssstttt vvvvooooiiiidddd ****,,,, ccccoooonnnnsssstttt vvvvooooiiiidddd ****))))))));;;;
-
- DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
- _l_s_e_a_r_c_h is a linear search routine generalized from Knuth (6.1) Algorithm
- S. It returns a pointer into a table indicating where a datum may be
- found. If the datum does not occur, it is added at the end of the table.
- KKKKeeeeyyyy points to the datum to be sought in the table. BBBBaaaasssseeee points to the
- first element in the table. NNNNmmmmeeeemmmmbbbb points to an integer containing the
- current number of elements in the table. The integer is incremented if
- the datum is added to the table. _S_i_z_e is the size of the key in bytes
- (_s_i_z_e_o_f (*_k_e_y)). CCCCoooommmmppppaaaarrrr is the name of the comparison function which the
- user must supply (_s_t_r_c_m_p, for example). It is called with two arguments
- that point to the elements being compared. The function must return zero
- if the elements are equal and non-zero otherwise.
-
- _L_f_i_n_d is the same as _l_s_e_a_r_c_h except that if the datum is not found, it is
- not added to the table. Instead, a NULL pointer is returned.
-
- NNNNOOOOTTTTEEEESSSS
- The pointers to the key and the element at the base of the table should
- be of type pointer-to-element, and cast to type pointer-to-character.
- The comparison function need not compare every byte, so arbitrary data
- may be contained in the elements in addition to the values being
- compared.
- Although declared as type pointer-to-character, the value returned should
- be cast into type pointer-to-element.
-
- EEEEXXXXAAAAMMMMPPPPLLLLEEEE
- This fragment will read in less than TABSIZE strings of length less than
- ELSIZE and store them in a table, eliminating duplicates.
-
- #include <stdio.h>
- #include <search.h>
-
- #define TABSIZE 50
- #define ELSIZE 120
-
- char line[ELSIZE], tab[TABSIZE][ELSIZE], *lsearch( );
- unsigned nel = 0;
-
-
-
- PPPPaaaaggggeeee 1111
-
-
-
-
-
-
- LLLLSSSSEEEEAAAARRRRCCCCHHHH((((3333CCCC)))) LLLLSSSSEEEEAAAARRRRCCCCHHHH((((3333CCCC))))
-
-
-
- int strcmp( );
- . . .
- while (fgets(line, ELSIZE, stdin) != NULL &&
- nel < TABSIZE)
- (void) lsearch(line, (char *)tab, &nel,
- ELSIZE, strcmp);
- . . .
-
- SSSSEEEEEEEE AAAALLLLSSSSOOOO
- bsearch(3C), hsearch(3C), string(3C), tsearch(3C).
-
- DDDDIIIIAAAAGGGGNNNNOOOOSSSSTTTTIIIICCCCSSSS
- If the searched for datum is found, both _l_s_e_a_r_c_h and _l_f_i_n_d return a
- pointer to it. Otherwise, _l_f_i_n_d returns NULL and _l_s_e_a_r_c_h returns a
- pointer to the newly added element.
-
- BBBBUUUUGGGGSSSS
- Undefined results can occur if there is not enough room in the table to
- add a new item.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- PPPPaaaaggggeeee 2222
-
-
-
-